package com.congee02;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * @author congee(congee02 @ 163.com)
 * @date 1/28/2023 6:33 PM
 */
public class WeakReferenceTest {

    // 常用于缓存
    public static void main(String[] args) {
        // gc 时，不管内存是否足够，该引用都是会被回收
        Reference<WeakSampleObject> reference = getWeakReference();
        System.gc();
        System.out.println("gc done");
    }
    
    private static Reference<WeakSampleObject> getWeakReference() {
        return new WeakReference<>(new WeakSampleObject());
    }

    private static class WeakSampleObject {
        @Override
        protected void finalize() throws Throwable {
            System.out.println("WeakSampleObject: doing finalize");
        }
    }
    
}
